package com.gwtapps.multisearch.client;

import java.util.ArrayList;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.json.client.JSONString;
import com.google.gwt.user.client.Window;
import com.gwtapps.client.util.JSONRequest;
import com.gwtapps.client.util.JSONRequestHandler;
import com.gwtapps.multisearch.client.model.SearchEngine;
import com.gwtapps.multisearch.client.model.SearchEngineResult;
import com.gwtapps.multisearch.client.view.MultiSearchView;

public class YouTubeSearcher extends Searcher implements JSONRequestHandler {
	
	public YouTubeSearcher( MultiSearchView view ){
		super( view, new SearchEngine( "YouTube Search", "http://www.youtube.com" ) );
	}
	
	public void query( String q ){
		String[] splittedKeyword = q.split(" ");
		String querytemp="";
		String query="";
		for (int i=0; i < splittedKeyword.length; i++)
		{
			querytemp="/"+splittedKeyword[i]+querytemp;
		}
		query="http://gdata.youtube.com/feeds/api/videos/-"+querytemp+"?alt=json-in-script&callback=";
		JSONRequest.get( query, this  );
		getView().clearResults();
	}
	
	public void onRequestComplete( JavaScriptObject json ){
		JSONObject j = new JSONObject(json);
		JSONObject feed = (JSONObject)j.get("feed");
		//total result//
		JSONObject totalResultObj = (JSONObject)feed.get("openSearch$totalResults");
		JSONString totalResult = (JSONString)totalResultObj.get("$t");
		//Window.alert(totalResult.stringValue());
		
		JSONArray entries = (JSONArray)feed.get("entry");
		
		
		for( int i=0; i< entries.size(); ++i ){
			JSONObject entry = (JSONObject)entries.get(i);
			
			
			//title//
			JSONObject titleObj = (JSONObject)entry.get("title");
			JSONString title = (JSONString)titleObj.get("$t");
			
			//youtube watch link//
			JSONArray links = (JSONArray)entry.get("link");
			String link = null;
			for( int k = 0; k < links.size(); ++k ) {
				JSONObject linkObj = (JSONObject)links.get(k);
				JSONString linkType = (JSONString)linkObj.get("rel");
				if( "alternate".compareTo(linkType.stringValue()) == 0 ) {
					JSONString linkString = (JSONString)linkObj.get("href");
					link = linkString.stringValue();
					break;
				}
			}
			
			//description//
			JSONObject contentObj = (JSONObject)entry.get("content");
			JSONString content = (JSONString)contentObj.get("$t");
			
//			author
			JSONArray authorArray = (JSONArray)entry.get("author");
			JSONObject authorObj = (JSONObject)authorArray.get(0);
			JSONObject authorNameObj = (JSONObject)authorObj.get("name");
			JSONString author = (JSONString)authorNameObj.get("$t");
			
			//published date//
			JSONObject publishedObj = (JSONObject)entry.get("published");
			JSONString published = (JSONString)publishedObj.get("$t");
			
//			mediaGroup//
			JSONObject mediaGroupObj = (JSONObject)entry.get("media$group");
			
			
			//thumbnail
			JSONArray thumbnailArray = (JSONArray)mediaGroupObj.get("media$thumbnail");
			JSONObject t1 = (JSONObject)thumbnailArray.get(0);
			JSONObject t2 = (JSONObject)thumbnailArray.get(1);
			JSONObject t3 = (JSONObject)thumbnailArray.get(2);
			JSONObject t4 = (JSONObject)thumbnailArray.get(3);
			
			JSONString tt1=(JSONString)t1.get("url");
			JSONString tt2=(JSONString)t2.get("url");
			JSONString tt3=(JSONString)t3.get("url");
			JSONString tt4=(JSONString)t4.get("url");
			
			
			
			
			String thumbnailURL1=tt1.stringValue();
			String thumbnailURL2=tt2.stringValue();
			String thumbnailURL3=tt3.stringValue();
			String thumbnailURL4=tt4.stringValue();
			
			//category
			JSONArray categoryArray = (JSONArray)mediaGroupObj.get("media$category");
			JSONString category = null;
			for( int k = 0; k < categoryArray.size(); ++k ) {
				
				JSONObject categoryObj = (JSONObject)categoryArray.get(k);
				category = (JSONString)categoryObj.get("$t");
				
			}
			
			
			
			//keywords//
			JSONObject keywordsObj = (JSONObject)mediaGroupObj.get("media$keywords");
			JSONString keywords = (JSONString)keywordsObj.get("$t");
			
			
			
			//viewCount
			JSONString viewCount;
			JSONObject statisticsObj = (JSONObject)entry.get("yt$statistics");
			if (statisticsObj == null){
				viewCount = new JSONString("0");
			}
			else{
			viewCount = (JSONString)statisticsObj.get("viewCount");
			}
			
			//favoriteCount
			JSONString favoriteCount = (JSONString)statisticsObj.get("favoriteCount");
			if (favoriteCount == null){
				favoriteCount = new JSONString("0");
			}
			
			//numRaters
			JSONObject ratingObj = (JSONObject)entry.get("gd$rating");
			JSONString numRaters;
			JSONString average;
			if (ratingObj == null){
				numRaters = new JSONString("0");
				String sss=numRaters.stringValue();
				average = new JSONString("0");
			}
			else{
			numRaters = (JSONString)ratingObj.get("numRaters");
			
			//average
			average = (JSONString)ratingObj.get("average");
			}
			
			
			
			
			if( link != null ){
				getView().addSearchResult(
						new SearchEngineResult( title.stringValue(),link, content.stringValue(),author.stringValue(),published.stringValue(), category.stringValue(),keywords.stringValue(), viewCount.stringValue(),favoriteCount.stringValue(),numRaters.stringValue(),average.stringValue(),thumbnailURL1,thumbnailURL2,thumbnailURL3,thumbnailURL4) );
			}
		}
	}
}
